package com.mango.amazon.util;// UploadExample.java
// This example is for use with the Selling Partner API for Feeds, Version: 2021-06-30

import com.squareup.okhttp.*;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;

/**
 * Example that uploads content.
 */
public class UploadOrDownloadExample {


    /**
     * Example that downloads a document.
     */
    /**
     * Download and optionally decompress the document retrieved from the given url.
     *
     * @param url                  the url pointing to a document
     * @param compressionAlgorithm the compressionAlgorithm used for the document
     * @throws IOException              when there is an error reading the response
     * @throws IllegalArgumentException when the charset is missing
     */
    public static void download(String url, String compressionAlgorithm) throws IOException, IllegalArgumentException {
        OkHttpClient httpclient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        Response response = httpclient.newCall(request).execute();
        if (!response.isSuccessful()) {
            System.out.println(
                    String.format("Call to download content was unsuccessful with response code: %d and message: %s",
                            response.code(), response.message()));
            return;
        }

        // System.out.println(response.body().string());
        //解密
        /*byte[] uncompress = uncompress(response.body().bytes());
        System.out.println(new String(uncompress));*/


        InputStream inputStream = response.body().byteStream();
        if (StringUtils.isNotBlank(compressionAlgorithm)){
            inputStream = new GZIPInputStream(inputStream);
        }

        String stringByInputStream_11 = getStringByInputStream_1(inputStream);
        System.out.println(stringByInputStream_11);

    }


    public static String getStringByInputStream_1(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            byte[] b = new byte[10240];
            int n;
            while ((n = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, n);
            }
        } catch (Exception e) {
            try {
                inputStream.close();
                outputStream.close();
            } catch (Exception e1) {
            }
        }
        return outputStream.toString();
    }

    /**
     * Upload content to the given URL.
     *
     * @param content the content to upload
     * @param url     the URL to upload content
     */
    public static void upload(String content, String url) {
        byte[] source = content.getBytes(StandardCharsets.UTF_8);

        OkHttpClient client = new OkHttpClient();

        // The contentType must match the input provided to the createFeedDocument operation. This example uses text/xml, but your contentType may be different depending upon on your chosen feedType (text/plain, text/csv, and so on).
        String contentType = String.format("text/xml; charset=%s", StandardCharsets.UTF_8);
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .put(RequestBody.create(MediaType.parse(String.format("text/xml; charset=%s", StandardCharsets.UTF_8)), source))
                    .build();

            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                System.out.println(
                        String.format("Call to upload document failed with response code: %d and message: %s",
                                response.code(), response.message()));
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }


}